/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servidor.Controlador;

import Servidor.Modelo.Games.Game;
import Servidor.Modelo.Games.GameList;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * This class creates a Thread for get the active games
 * @author Jorge Perez
 */
public class ListActiveGamesTask extends Thread implements OcaTask {

    private Socket socket;
    private BufferedReader read;
    private PrintWriter write;

    public ListActiveGamesTask(Socket socket, BufferedReader read, PrintWriter write) {

        this.socket = socket;
        this.read = read;
        this.write = write;
    }

    @Override
    public void run() {
        ArrayList<Game> games = GameList.getInstance().getLista_partidas();
        Iterator<Game> iter = games.iterator();
        
        while(iter.hasNext()){
            Game game = iter.next();
            String gameString = null;
            
            gameString = String.valueOf(game.getId_game()) + "#";            
            gameString += game.getName_game() + "#";
            gameString += String.valueOf(game.number_player());
            
            write.println(gameString);
        }
        write.println(ConnectionConstraints.LIST_FINISH);
    }

    @Override
    public void executeTask() {
        this.start();
    }
}
